package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableInt;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.lxj.xpopup.XPopup;
import com.weimei.conversionTreasure.R;
import com.weimei.conversionTreasure.activity.AddPfActivity;
import com.weimei.conversionTreasure.adapter.PfRecordAdapter;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.http.entity.FormulaListBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.weight.WarnPop;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class PfRecordVM extends TitleVM {
    public PfRecordVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("配方记录");
    }

    Activity mActivity;

    @Override
    public void setActivity(Activity activity) {
        super.setActivity(activity);
        mActivity = activity;
    }

    PfRecordAdapter pfRecordAdapter;

    public ObservableInt empty = new ObservableInt(View.VISIBLE);

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        pfRecordAdapter = new PfRecordAdapter();
        mpfRecy.setAdapter(pfRecordAdapter);

        pfRecordAdapter.addChildClickViewIds(R.id.item);
        pfRecordAdapter.addChildLongClickViewIds(R.id.item);

        pfRecordAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            FormulaListBean.DataDTO bean = (FormulaListBean.DataDTO) adapter.getData().get(position);
            Bundle bundle = new Bundle();
            bundle.putString("id", bean.getId() + "");
            bundle.putString("title", bean.getTitle());
            bundle.putString("content", bean.getContent());
            startActivity(AddPfActivity.class, bundle);
        });


        pfRecordAdapter.setOnItemChildLongClickListener((adapter, view, position) -> {
            FormulaListBean.DataDTO bean = (FormulaListBean.DataDTO) adapter.getData().get(position);
            new XPopup.Builder(mActivity)
                    .enableDrag(true)
                    .asCustom(new WarnPop(mActivity, (s) ->
                            del(bean.getId() + "")
                    )).show();
            return false;
        });
    }

    private void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("user_id", UserUtils.getUserAccount());
        model.formulalist(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {
                })
                .subscribe(new DisposableObserver<FormulaListBean>() {
                    @Override
                    public void onNext(FormulaListBean listBean) {
                        if (listBean.issucc) {
                            pfRecordAdapter.setList(listBean.getData());
                            if (listBean.getData().size() > 0) {
                                empty.set(View.GONE);
                            } else {
                                empty.set(View.VISIBLE);
                            }
                        } else {
                            empty.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void del(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("sid", id);
        model.delete_formula(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {
                })
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult listBean) {
                        if (listBean.issucc) {
                            ToastUtils.showShort(listBean.msg);
                            onResume();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


    RecyclerView mpfRecy;

    public void setView(RecyclerView pfRecy) {
        mpfRecy = pfRecy;
    }
}
